package com.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.dao.ApplyDao;
import com.book.dto.ApplyDto;
import com.book.dto.ApplyPage;
import com.book.entity.Apply;
import com.book.service.ApplyService;
import com.book.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

//这个类错误码从10001开始，注意不是1001
@Service
public class ApplyServiceImpl implements ApplyService {
    @Autowired
    private ApplyDao applyDao;
    @Override
    public ResponseResult getFirst(int postId,int currentPage,int pageSize) {
        try {
            PageHelper.startPage(currentPage,pageSize);
            List<ApplyDto> applyDtoList = applyDao.getFirst(postId);
            PageInfo<ApplyDto> pageInfo = new PageInfo<>(applyDtoList);
//            System.out.println(pageInfo);
            if(currentPage>pageInfo.getPages()){
                PageHelper.startPage(pageInfo.getPages(),pageSize);
                applyDtoList = applyDao.getFirst(postId);
                pageInfo = new PageInfo<>(applyDtoList);
            }
            return new ResponseResult(200,"获取评论成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(10001,"获取评论失败");
        }
    }

    @Override
    public ResponseResult getSecond(int postId, int parentApply) {
        try {
            return new ResponseResult(200,"获取成功",applyDao.getSecond(postId, parentApply));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(10002,"获取二级评论回复失败");
        }
    }

    @Override
    public ResponseResult addApply(Apply apply) {
        try {
            apply.setApplyTime(new Date());
            applyDao.insert(apply);
            return new ResponseResult(200,"评论成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(10003,"评论失败");
        }
    }

    /**
     * 获取自己的所有主动回复，按一级二级条件查询
     * @param applyPage
     * @return
     */
    @Override
    public ResponseResult getSelfApply(ApplyPage applyPage) {
        try {
            PageHelper.startPage(applyPage.getCurrentPage(),applyPage.getPageSize());
            List<ApplyDto> selfApply = applyDao.getSelfApply(applyPage.getLevel(), applyPage.getUserId());
            PageInfo<ApplyDto> applyDtoPageInfo = new PageInfo<>(selfApply);

            if(applyPage.getCurrentPage()>applyDtoPageInfo.getPages()){
                PageHelper.startPage(applyDtoPageInfo.getPages(),applyPage.getPageSize());
                selfApply = applyDao.getSelfApply(applyPage.getLevel(), applyPage.getUserId());
                applyDtoPageInfo = new PageInfo<>(selfApply);
            }
            return new ResponseResult(200,"获取成功",applyDtoPageInfo);
        }catch (Exception e){
            return new ResponseResult(10004,"获取回复失败");
        }
    }

    @Override
    public ResponseResult deleteMyApply(int id) {
        try {
            applyDao.deleteById(id);
            return new ResponseResult(200, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(10005,"删除失败");
        }
    }

    /**
     * 获取自己的所有被回复，按一级二级条件查询
     * @param applyPage
     * @return
     */
    @Override
    public ResponseResult getApplyMe(ApplyPage applyPage) {
        try {
            PageHelper.startPage(applyPage.getCurrentPage(),applyPage.getPageSize());
            List<ApplyDto> applyMe = applyDao.getApplyMe(applyPage.getLevel(), applyPage.getUserId());
            PageInfo<ApplyDto> applyDtoPageInfo = new PageInfo<>(applyMe);
            if(applyPage.getCurrentPage()>applyDtoPageInfo.getPages()){
                PageHelper.startPage(applyDtoPageInfo.getPages(),applyPage.getPageSize());
                applyMe = applyDao.getApplyMe(applyPage.getLevel(), applyPage.getUserId());
                applyDtoPageInfo = new PageInfo<>(applyMe);
            }
            return new ResponseResult(200,"获取成功",applyDtoPageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(10006,"获取回复失败");
        }
    }


    /**
     * 管理员获取所有的回复
     * @param applyPage
     * @return
     */
    @Override
    public ResponseResult getApplies(ApplyPage applyPage) {
        try {
            QueryWrapper<Apply> qw = new QueryWrapper<>();
            if(applyPage.getPostId()!=null){//如果传来了帖子id就比较
                qw.eq("post_id",applyPage.getPostId());
            }
            qw.like("user_id",applyPage.getUserId());//比较user_id
            qw.like("content",applyPage.getContent());//比较回复内容
            qw.orderByDesc("apply_time");
            PageHelper.startPage(applyPage.getCurrentPage(),applyPage.getPageSize());
            List<Apply> applyList = applyDao.selectList(qw);
            PageInfo<Apply> applyPageInfo = new PageInfo<>(applyList);
            if(applyPage.getCurrentPage()>applyPageInfo.getPages()){
                PageHelper.startPage(applyPageInfo.getPages(),applyPage.getPageSize());
                applyList = applyDao.selectList(qw);
                applyPageInfo = new PageInfo<>(applyList);
            }
            return new ResponseResult(200,"获取成功",applyPageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(10007,"获取回复失败");
        }
    }


    /**
     * 管理员删除一个回复，那么如果这个回复被其他人回复也一起删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteApply(int id) {
        try {
            applyDao.deleteById(id);//删除回复
            QueryWrapper<Apply> qw = new QueryWrapper<>();
            //删除这个回复的子回复
            qw.eq("parent_apply",id);
            applyDao.delete(qw);
            return new ResponseResult(200,"删除回复成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(10008,"删除回复失败");
        }
    }
}
